const co = require('co')
const {
	select,
	insert,
	insertBatch,
	update,
	executeSQL,
} = require('../db-mysql')

const table = 'tb_bug'
const fields = [
	'id',
	'description',
	'project_version',
	'user_id',
	'project_id',
	'create_date'
]

module.exports = {

	submitBug(params) {
		let values = []
		let addFields = fields.filter((val, idx) => {
			if (val == 'id')
				return false
			if (val == 'create_date')
				values.push("NOW()")
			else
				values.push(`'${params[val] || ''}'`)
			return true
		});
		return insert({
			table,
			fields: addFields,
			values,
		})
	},

	bugList() {
		let SQL = 'SELECT bug.id, bug.description, bug.project_version, bug.create_date, tp.name AS project_name, tu.realname AS create_user FROM tb_bug bug'
		SQL += ' LEFT JOIN tb_project tp ON bug.project_id = tp.id'
		SQL += ' LEFT JOIN tb_user tu ON tu.id = bug.user_id'
		SQL += ' ORDER BY bug.id DESC'
		return executeSQL(SQL)
	}
}